package exercises.leetcode;

import java.util.ArrayDeque;
import java.util.HashSet;

/**
 * <a href="">
 * </a>
 *
 * <p><span color="#87CEEB">描述:</span>
 *
 * @author or2
 * @date 2021年10月31日 时间: 10:54
 */
public class No5916 {
    public int minimumOperations(int[] nums, int start, int goal) {
        var vis = new HashSet<Integer>();
        var queue = new ArrayDeque<Integer>();
        int res = 0, last = start;

        queue.add(start);
        vis.add(start);
        while (!queue.isEmpty()) {
            int remove = queue.remove();
            if (remove == goal) return res;

            if (remove >= 0 && remove <= 1000) {
                for (int num : nums) {
                    int add = num + remove;
                    if (!vis.contains(add)) {
                        if (add == goal) return res + 1;
                        if (add >= 0 && add <= 1000) {
                            queue.add(add);
                            vis.add(add);
                        }
                    }
                    int sub = remove - num;
                    if (!vis.contains(sub)) {
                        if (sub == goal) return res + 1;
                        if (sub >=0 && sub <= 1000) {
                            queue.add(sub);
                            vis.add(sub);
                        }
                    }
                    int xor = remove ^ num;
                    if (!vis.contains(xor)) {
                        if (xor == goal) return res + 1;
                        if (xor >= 0 && xor <= 1000) {
                            queue.add(xor);
                            vis.add(xor);
                        }
                    }
                }
            }

            if (!queue.isEmpty() && remove == last) {
                res++;
                last = queue.getLast();
            }
        }
        return -1;
    }

}

class test {
    public static void main(String[] args) {
        No5916 no5916 = new No5916();
//        System.out.println(no5916.minimumOperations(new int[]{-31, 15, 23, 43, 56, 21, -65, 45, 22, -85}, 68, -42));
        System.out.println(no5916.minimumOperations(new int[]{1}, 0, 3));
    }
}
